<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: #f0f0f0;
            flex-direction: column;
        }

        .box {
            width: 100%;
            height: 300px;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
        }

        .log {
            width: 300px;
            height: 200px;
            display: flex;
            align-items: center;
        }

        .log img {
            width: 260px;
            height: 100px;
        }

        .input {
            border: 1px solid #ccc;
            display: flex;
            border-top-left-radius: 8px;
            border-bottom-left-radius: 8px;
            border-top-right-radius: 8px;
            border-bottom-right-radius: 8px;
        }

        .input input {
            width: 300px;
            height: 43px;
            border: none;
            outline: none;
            border-top-left-radius: 8px;
            border-bottom-left-radius: 8px;
        }

        .input div {
            width: 60px;
            height: 45px;
            display: flex;
            justify-content: center;
            align-items: center;
            color: #999;
            background-color: #fff;
            font-size: 12px;
        }

        .input span {
            width: 89px;
            height: 45px;
            background-color: #08f;
            color: #fff;
            display: flex;
            justify-content: center;
            align-items: center;
            border-top-right-radius: 8px;
            border-bottom-right-radius: 8px;
        }

        .xiala {
            list-style: none;
            padding: 5px 0;
            width: 100px;
            position: absolute;
            margin: 0;
            line-height: 26px;
            font-size: 12px;
            border-radius: 2px;
            box-shadow: 0 1px 5px rgba(0, 0, 0, .2);
            background-color: #fff;
            margin-top: 370px;
            margin-left: 165px;
            display: none;
            /* 默认隐藏 */
        }

        .xiala img {
            width: 17px;
            height: 17px;
            margin-left: -5px;
        }

        .xiala li {
            font-size: 12px;
            display: flex;
            align-items: center;
            justify-content: space-evenly;
            list-style-type: none;
        }

        .xiala li:hover {
            background-color: #eee;
        }

        .tt img {
            margin-left: -20px;
            margin-right: -10px;
        }

        #result {
            padding: 10px 0;
            width: 360px;
            position: absolute;
            margin: 0;
            line-height: 32px;
            font-size: 14px;
            border-radius: 2px;
            box-shadow: 0 1px 5px rgba(0, 0, 0, .2);
            background-color: #fff;
            margin-top: 597px;
            margin-left: -88px;
            display: none;

            & a {
                display: flex;
                text-decoration: none;
            }

            & a:hover {
                background-color: #eee;
            }

            & div {
                margin-left: 27px;
                color: #000;
            }
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="log"><img id="logoImage" src="./img/baidu.png.png" alt=""></div>
        <div class="input">
            <input type="text" id="searchInput" placeholder="请输入要搜索的内容">
            <div id="xiala">百度 v</div><span id="searchButton">搜索</span>
        </div>
    </div>
    <ul class="xiala">
        <li class="tt" data-src="./img/baidu.png.png" data-text="百度 v"><img src="./img/百度.png.png" alt="">百度</li>
        <li class="tt" data-src="./img/sougou.png.png" data-text="搜狗 v"><img src="./img/搜狗.png.png" alt="">搜狗</li>
        <li class="tt" data-src="./img/bing.log.png" data-text="bing v"><img src="./img/bing.png.png" alt="">bing</li>
        <li class="tt" data-src="./img/guge.png.png" data-text="Google v"><img src="./img/googl.ico" alt="">Google</li>
    </ul>
    <div id="result">
        <a href="">
            <div style="color: #ff2c00">1</div>
            <div>helang.love@qq.com</div>
        </a>
        <a href="">
            <div style="color: #ff5a00">2</div>
            <div>qq:1846492969</div>
        </a>
        <a href="">
            <div style="color: #ff8105">3</div>
            <div>web前端梦之蓝</div>
        </a>
        <a href="">
            <div style="color: #fd9a15">4</div>
            <div>公众号:web-7258</div>
        </a>
        <a href="">
            <div style="color: #dfad1c">5</div>
            <div>jQuery插件库</div>
        </a>
        <a href="">
            <div style="color: #6bc211">6</div>
            <div>CSDN-IT博客</div>
        </a>
        <a href="">
            <div style="color: #3cc71e">7</div>
            <div>jQuery之美-CSDN博客专栏</div>
        </a>
        <a href="">
            <div style="color: #3cbe85">8</div>
            <div>jq22.com</div>
        </a>
        <a href="">
            <div style="color: #51b2ef">9</div>
            <div>csdn.net</div>
        </a>
        <a href="">
            <div style="color: #53b0ff;margin-left: 22px; ">10</div>
            <div style="margin-left: 23px;">mydarling.gitee.io</div>
        </a>
    </div>
</body>
<script>
    // 获取元素
    const xiala = document.getElementById('xiala');
    const xialaList = document.querySelector('.xiala');
    const searchInput = document.getElementById('searchInput');
    const searchButton = document.getElementById('searchButton');
    const logoImage = document.getElementById('logoImage');
    const resultDiv = document.getElementById('result');

    // 控制下拉菜单的显示和隐藏
    xiala.addEventListener('click', () => {
        if (xialaList.style.display === 'none') {
            xialaList.style.display = 'block';
        } else {
            xialaList.style.display = 'none';
        }
    });

    // 实现百度搜索功能
    searchButton.addEventListener('click', () => {
        const query = searchInput.value.trim();
        if (query) {
            window.location.href = `https://www.baidu.com/s?wd=${encodeURIComponent(query)}`;
        }
    });

    // 监听 focus 和 blur 事件来控制 resultDiv 的显示与隐藏
    searchInput.addEventListener('focus', () => {
        resultDiv.style.display = 'block'; // 输入框获得焦点时显示 resultDiv
    });

    searchInput.addEventListener('blur', () => {
        resultDiv.style.display = 'none'; // 输入框失去焦点时隐藏 resultDiv
    });

    // 更换搜索引擎并更新 logo 图片
    xialaList.addEventListener('click', (event) => {
        if (event.target.classList.contains('tt')) {
            const newSrc = event.target.getAttribute('data-src');
            const newText = event.target.getAttribute('data-text');
            logoImage.src = newSrc;
            xiala.textContent = newText;

            // 更新搜索按钮的点击事件
            searchButton.removeEventListener('click', searchBaidu);
            searchButton.addEventListener('click', () => {
                const query = searchInput.value.trim();
                if (query) {
                    switch (newSrc) {
                        case './img/百度.png.png':
                            window.location.href = `https://www.baidu.com/s?wd=${encodeURIComponent(query)}`;
                            break;
                        case './img/搜狗.png.png':
                            window.location.href = `https://www.sogou.com/web?query=${encodeURIComponent(query)}`;
                            break;
                        case './img/bing.png.png':
                            window.location.href = `https://www.bing.com/search?q=${encodeURIComponent(query)}`;
                            break;
                        case './img/googl.ico':
                            window.location.href = `https://www.google.com/search?q=${encodeURIComponent(query)}`;
                            break;
                        default:
                            window.location.href = `https://www.baidu.com/s?wd=${encodeURIComponent(query)}`;
                    }
                }
            });
            // 点击选项后隐藏下拉菜单
            xialaList.style.display = 'none';
        }
    });

    // 初始搜索按钮点击事件
    function searchBaidu() {
        const query = searchInput.value.trim();
        if (query) {
            window.location.href = `https://www.baidu.com/s?wd=${encodeURIComponent(query)}`;
        }
    }
    searchButton.addEventListener('click', searchBaidu);
</script>

</html>